package util;

import dao.HospitalDao;
import model.Hospital;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import view.ImportReturnView;
import view.ImportView;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by gable on 2018/5/22.
 */
@Component
public class CheckImportUtil {

    @Autowired
    HospitalDao hospitalDao;

    //年份月份组合字符串作为key
    public static Map<String,Object> resultMap = new ConcurrentHashMap();

    private String REGNUMBER = "^[0-9]\\d*(\\.\\d{1,8})?$";

    private String REGINTEGER = "^\\+?[0-9]\\d*$";

    public static String [] properties = new String [] {
            "hospital","staffNum","permanentStaff","nonStaff","staffAnnualExpectedWage",
            "staffPerCapitaWage","staffWageImplementationRate","annualExpectedHumanCost",
            "humanCost","humanCostImplementationRate","medicalIncome","medicareIncome","drugIncome",
            "medicalExpenditure","humanExpenditure","drugExpenditure","outpatientPersontime",
            "outpatientReturnRate","outpatientPerCapitaCost", "outpatientMedicineRate","discharges",
            "hospitalizationMedicineRate","medicineRate","hygienicMaterialRate","medicalEvent", "administrativePenaltyCases",
            "actualSettlement", "averageStayLength","averageHospitalizationCost", "level3Level4OperationRate",
            "selectiveOperationRate","bloodPressure","feverClinicPersontime","safetyAccident","smallProjectPractice","midtermAbortion",
            "midtermAbortionSexRate", "brithSexRate","actual","actualRate"
    };


    public  ImportReturnView checkImport(List<ImportView> list) {
        //给到客户端的数据集
        ImportReturnView irv = new ImportReturnView();
        //错误信息
        List<String> errorMessage = new ArrayList<String>();
        int count = 0;
        if (list == null || list.size() == 0) {
            return null;
        }

        for (int i = 0; i < list.size(); i++) {
             int row = i + 2;

            //医院信息
            if(list.get(i).getHospital() == null || list.get(i).getHospital().trim().equals("")){
                errorMessage.add("第"+row+"行，第"+queryProperties("hospital")+"列的医院信息不能为空");
                count++;
            }else{
                Hospital hospital = hospitalDao.checkHospital(list.get(i).getHospital());
                if(hospital == null) {
                    errorMessage.add("第" + row + "行，第" + queryProperties("hospital") + "列的医院信息不存在");
                    count++;
                }else{
                    list.get(i).setHospitalId(hospital.getId());
                }
            }
            //医院分类信息无须校验


            //----------------------------人员编制--------------------------------------
            //编制数
            if(!StringUtils.isEmpty(list.get(i).getStaffNum())){
                if(!checkPattern(REGINTEGER,list.get(i).getStaffNum())){
                    errorMessage.add("第"+row+"行，第"+queryProperties("staffNum")+"列的编制数格式不正确");
                    count++;
                }
            }

            //在编人数（人）
            if(!StringUtils.isEmpty(list.get(i).getPermanentStaff())){
                if(!checkPattern(REGINTEGER,list.get(i).getPermanentStaff())){
                    errorMessage.add("第"+row+"行，第"+queryProperties("permanentStaff")+"列的在编人数格式不正确");
                    count++;
                }
            }

            //非在编人数（人）
            if(!StringUtils.isEmpty(list.get(i).getNonStaff())){
                if(!checkPattern(REGINTEGER,list.get(i).getNonStaff())){
                    errorMessage.add("第"+row+"行，第"+queryProperties("nonStaff")+"列的非在编人数格式不正确");
                    count++;
                }
            }

            //----------------------------人员绩效--------------------------------------
            //在编人均绩效工资年度期望值(元)
            if(!StringUtils.isEmpty(list.get(i).getStaffAnnualExpectedWage())){
                if(!checkPattern(REGNUMBER,list.get(i).getStaffAnnualExpectedWage())){
                    errorMessage.add("第"+row+"行，第"+queryProperties("staffAnnualExpectedWage")+"列的在编人均绩效工资年度期望值格式不正确");
                    count++;
                }
            }

            //在编人均绩效工资
            if(!StringUtils.isEmpty(list.get(i).getStaffPerCapitaWage())){
                if(!checkPattern(REGNUMBER,list.get(i).getStaffPerCapitaWage())){
                    errorMessage.add("第"+row+"行，第"+queryProperties("staffPerCapitaWage")+"列的在编人均绩效工资格式不正确");
                    count++;
                }
            }

            //在编人均绩效工资执行率(%)
            if(!StringUtils.isEmpty(list.get(i).getStaffWageImplementationRate())){
                if(!checkPattern(REGNUMBER,list.get(i).getStaffWageImplementationRate())){
                    errorMessage.add("第"+row+"行，第"+queryProperties("staffWageImplementationRate")+"列的在编人均绩效工资执行率(%)格式不正确");
                    count++;
                }
            }

            //人力成本总额年度期望值(万元)
            if(!StringUtils.isEmpty(list.get(i).getAnnualExpectedHumanCost())){
                if(!checkPattern(REGNUMBER,list.get(i).getAnnualExpectedHumanCost())){
                    errorMessage.add("第"+row+"行，第"+queryProperties("annualExpectedHumanCost")+"列的人力成本总额年度期望值(万元)格式不正确");
                    count++;
                }
            }

            //人力成本总额(万元)
            if(!StringUtils.isEmpty(list.get(i).getHumanCost())){
                if(!checkPattern(REGNUMBER,list.get(i).getHumanCost())){
                    errorMessage.add("第"+row+"行，第"+queryProperties("humanCost")+"列的人力成本总额(万元)格式不正确");
                    count++;
                }
            }

            //人力成本预算执行率(%)
            if(!StringUtils.isEmpty(list.get(i).getHumanCostImplementationRate())){
                if(!checkPattern(REGNUMBER,list.get(i).getHumanCostImplementationRate())){
                    errorMessage.add("第"+row+"行，第"+queryProperties("humanCostImplementationRate")+"列的人力成本预算执行率(%)格式不正确");
                    count++;
                }
            }

            //----------------------------收支情况--------------------------------------
            //医疗收入(万元)
            if(!StringUtils.isEmpty(list.get(i).getMedicalIncome())){
                if(!checkPattern(REGNUMBER,list.get(i).getMedicalIncome())){
                    errorMessage.add("第"+row+"行，第"+queryProperties("medicalIncome")+"列的医疗收入(万元)格式不正确");
                    count++;
                }
            }

            //医保病人收入(万元)
            if(!StringUtils.isEmpty(list.get(i).getMedicareIncome())){
                if(!checkPattern(REGNUMBER,list.get(i).getMedicareIncome())){
                    errorMessage.add("第"+row+"行，第"+queryProperties("medicareIncome")+"列的医保病人收入(万元)格式不正确");
                    count++;
                }
            }

            //药品收入(万元)
            if(!StringUtils.isEmpty(list.get(i).getDrugIncome())){
                if(!checkPattern(REGNUMBER,list.get(i).getDrugIncome())){
                    errorMessage.add("第"+row+"行，第"+queryProperties("medicareIncome")+"列的药品收入(万元)格式不正确");
                    count++;
                }
            }

            //医疗业务支出(万元)
            if(!StringUtils.isEmpty(list.get(i).getMedicalExpenditure())){
                if(!checkPattern(REGNUMBER,list.get(i).getMedicalExpenditure())){
                    errorMessage.add("第"+row+"行，第"+queryProperties("medicalExpenditure")+"列的医疗业务支出(万元)格式不正确");
                    count++;
                }
            }

            //人员支出(万元)
            if(!StringUtils.isEmpty(list.get(i).getHumanExpenditure())){
                if(!checkPattern(REGNUMBER,list.get(i).getHumanExpenditure())){
                    errorMessage.add("第"+row+"行，第"+queryProperties("humanExpenditure")+"列的人员支出(万元)格式不正确");
                    count++;
                }
            }

            //药品支出(万元)
            if(!StringUtils.isEmpty(list.get(i).getDrugExpenditure())){
                if(!checkPattern(REGNUMBER,list.get(i).getDrugExpenditure())){
                    errorMessage.add("第"+row+"行，第"+queryProperties("humanExpenditure")+"列的药品支出(万元)格式不正确");
                    count++;
                }
            }

            //----------------------------医疗业务--------------------------------------
            //门急诊人次(人次)
            if(!StringUtils.isEmpty(list.get(i).getOutpatientPersontime())){
                if(!checkPattern(REGINTEGER,list.get(i).getOutpatientPersontime())){
                    errorMessage.add("第"+row+"行，第"+queryProperties("outpatientPersontime")+"列的门急诊人次(人次)格式不正确");
                    count++;
                }
            }
            //门急诊复诊率
            if(!StringUtils.isEmpty(list.get(i).getOutpatientReturnRate())){
                if(!checkPattern(REGNUMBER,list.get(i).getOutpatientReturnRate())){
                    errorMessage.add("第"+row+"行，第"+queryProperties("outpatientReturnRate")+"列的门急诊复诊率格式不正确");
                    count++;
                }
            }
            //门急诊人月均费用(元)
            if(!StringUtils.isEmpty(list.get(i).getOutpatientPerCapitaCost())){
                if(!checkPattern(REGNUMBER,list.get(i).getOutpatientPerCapitaCost())){
                    errorMessage.add("第"+row+"行，第"+queryProperties("outpatientPersontime")+"列的门急诊人月均费用(元)格式不正确");
                    count++;
                }
            }
            //门诊药占比(%)
            if(!StringUtils.isEmpty(list.get(i).getOutpatientMedicineRate())){
                if(!checkPattern(REGNUMBER,list.get(i).getOutpatientMedicineRate())){
                    errorMessage.add("第"+row+"行，第"+queryProperties("outpatientMedicineRate")+"列的门诊药占比(%)格式不正确");
                    count++;
                }
            }
            //出院人次
            if(!StringUtils.isEmpty(list.get(i).getDischarges())){
                if(!checkPattern(REGINTEGER,list.get(i).getDischarges())){
                    errorMessage.add("第"+row+"行，第"+queryProperties("discharges")+"列的出院人次格式不正确");
                    count++;
                }
            }
            //住院药占比(%)
            if(!StringUtils.isEmpty(list.get(i).getHospitalizationMedicineRate())){
                if(!checkPattern(REGNUMBER,list.get(i).getHospitalizationMedicineRate())){
                    errorMessage.add("第"+row+"行，第"+queryProperties("hospitalizationMedicineRate")+"列的住院药占比(%)格式不正确");
                    count++;
                }
            }
            //药品收入占医疗收入比（%）
            if(!StringUtils.isEmpty(list.get(i).getMedicineRate())){
                if(!checkPattern(REGNUMBER,list.get(i).getMedicineRate())){
                    errorMessage.add("第"+row+"行，第"+queryProperties("medicineRate")+"列的药品收入占医疗收入比（%）格式不正确");
                    count++;
                }
            }
            //非药品收入中卫材比(%)
            if(!StringUtils.isEmpty(list.get(i).getHygienicMaterialRate())){
                if(!checkPattern(REGNUMBER,list.get(i).getHygienicMaterialRate())){
                    errorMessage.add("第"+row+"行，第"+queryProperties("hygienicMaterialRate")+"列的非药品收入中卫材比(%)格式不正确");
                    count++;
                }
            }
            //医疗事件
            if(!StringUtils.isEmpty(list.get(i).getMedicalEvent())){
                if(!checkPattern(REGINTEGER,list.get(i).getMedicalEvent())){
                    errorMessage.add("第"+row+"行，第"+queryProperties("medicalEvent")+"列的医疗事件格式不正确");
                    count++;
                }
            }
            //行政处罚案件数
            if(!StringUtils.isEmpty(list.get(i).getAdministrativePenaltyCases())){
                if(!checkPattern(REGINTEGER,list.get(i).getAdministrativePenaltyCases())){
                    errorMessage.add("第"+row+"行，第"+queryProperties("administrativePenaltyCases")+"列的行政处罚案件数格式不正确");
                    count++;
                }
            }
            //平均住院日（日）
            if(!StringUtils.isEmpty(list.get(i).getAverageStayLength())){
                if(!checkPattern(REGNUMBER,list.get(i).getAverageStayLength())){
                    errorMessage.add("第"+row+"行，第"+queryProperties("averageStayLength")+"列的平均住院日（日）格式不正确");
                    count++;
                }
            }
            //住院均次费用（万元）
            if(!StringUtils.isEmpty(list.get(i).getAverageHospitalizationCost())){
                if(!checkPattern(REGNUMBER,list.get(i).getAverageHospitalizationCost())){
                    errorMessage.add("第"+row+"行，第"+queryProperties("averageHospitalizationCost")+"列的住院均次费用（万元）格式不正确");
                    count++;
                }
            }
            //三、四级手术百分率（%）
            if(!StringUtils.isEmpty(list.get(i).getLevel3Level4OperationRate())){
                if(!checkPattern(REGNUMBER,list.get(i).getLevel3Level4OperationRate())){
                    errorMessage.add("第"+row+"行，第"+queryProperties("level3Level4OperationRate")+"列的三、四级手术百分率（%）格式不正确");
                    count++;
                }
            }
            //择期手术百分率(%)
            if(!StringUtils.isEmpty(list.get(i).getSelectiveOperationRate())){
                if(!checkPattern(REGNUMBER,list.get(i).getSelectiveOperationRate())){
                    errorMessage.add("第"+row+"行，第"+queryProperties("selectiveOperationRate")+"列的择期手术百分率(%)格式不正确");
                    count++;
                }
            }
            //35岁以上人群首诊测压人数
            if(!StringUtils.isEmpty(list.get(i).getBloodPressure())){
                if(!checkPattern(REGINTEGER,list.get(i).getBloodPressure())){
                    errorMessage.add("第"+row+"行，第"+queryProperties("bloodPressure")+"列的35岁以上人群首诊测压人数格式不正确");
                    count++;
                }
            }
            //发热门诊就诊人次数
            if(!StringUtils.isEmpty(list.get(i).getFeverClinicPersontime())){
                if(!checkPattern(REGINTEGER,list.get(i).getFeverClinicPersontime())){
                    errorMessage.add("第"+row+"行，第"+queryProperties("feverClinicPersontime")+"列的发热门诊就诊人次数格式不正确");
                    count++;
                }
            }
            //实际结算床日数
            if(!StringUtils.isEmpty(list.get(i).getActualSettlement())){
                if(!checkPattern(REGNUMBER,list.get(i).getActualSettlement())){
                    errorMessage.add("第"+row+"行，第"+queryProperties("actualSettlement")+"列的实际结算床日数格式不正确");
                    count++;
                }
            }
            //----------------------------运行保障--------------------------------------
            //安全生产事故发生数
            if(!StringUtils.isEmpty(list.get(i).getSafetyAccident())){
                if(!checkPattern(REGINTEGER,list.get(i).getSafetyAccident())){
                    errorMessage.add("第"+row+"行，第"+queryProperties("safetyAccident")+"列的安全生产事故发生数格式不正确");
                    count++;
                }
            }
            //限额以下小型建设项目实施数
            if(!StringUtils.isEmpty(list.get(i).getSmallProjectPractice())){
                if(!checkPattern(REGINTEGER,list.get(i).getSmallProjectPractice())){
                    errorMessage.add("第"+row+"行，第"+queryProperties("smallProjectPractice")+"列的限额以下小型建设项目实施数格式不正确");
                    count++;
                }
            }
            //----------------------------计生服务--------------------------------------
            //中期引产数（例）
            if(!StringUtils.isEmpty(list.get(i).getMidtermAbortion())){
                if(!checkPattern(REGINTEGER,list.get(i).getMidtermAbortion())){
                    errorMessage.add("第"+row+"行，第"+queryProperties("midtermAbortion")+"列的中期引产数（例）格式不正确");
                    count++;
                }
            }
            //中期引产性别比（%）
            if(!StringUtils.isEmpty(list.get(i).getMidtermAbortionSexRate())){
                if(!checkPattern(REGNUMBER,list.get(i).getMidtermAbortionSexRate())){
                    errorMessage.add("第"+row+"行，第"+queryProperties("midtermAbortionSexRate")+"列的中期引产性别比（%）格式不正确");
                    count++;
                }
            }
            //出生性别比
            if(!StringUtils.isEmpty(list.get(i).getBrithSexRate())){
                if(!checkPattern(REGNUMBER,list.get(i).getBrithSexRate())){
                    errorMessage.add("第"+row+"行，第"+queryProperties("brithSexRate")+"列的出生性别比格式不正确");
                    count++;
                }
            }
            //----------------------------全科医生--------------------------------------
            //全科医生
            if(!StringUtils.isEmpty(list.get(i).getActual())){
                if(!checkPattern(REGINTEGER,list.get(i).getActual())){
                    errorMessage.add("第"+row+"行，第"+queryProperties("actual")+"列的全科医生格式不正确");
                    count++;
                }
            }
            //千人口全科医生数
            if(!StringUtils.isEmpty(list.get(i).getActualRate())){
                if(!checkPattern(REGNUMBER,list.get(i).getActualRate())){
                    errorMessage.add("第"+row+"行，第"+queryProperties("actualRate")+"列的千人口全科医生数格式不正确");
                    count++;
                }
            }
        }
        if(count == 0){
            irv.setBool(true);
        }else{
            irv.setBool(false);
        }

        irv.setErrorMessage(errorMessage);
        return irv;
    }

    //获取Properties数组字符串的下标
    public int queryProperties(String str){
        if(str == null || str.equals("")){
            return 0;
        }
        for(int i = 0;i<properties.length;i++){
            if(properties[i].equals(str)){
                int index = i+1;
                return index;
            }
        }
        return 0;
    }

    //正则表达式
    public boolean  checkPattern(String reg,String str){
        str = str .replaceAll("/","-");
        Pattern pattern = Pattern.compile(reg);
        Matcher matcher = pattern.matcher(str);
        boolean bl = matcher.matches();
        return bl;
    }
}
